package com.chlitina.store.modules.sms.quartz;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chlitina.store.modules.base.entity.Customer;
import com.chlitina.store.modules.sms.entity.SmsStoreGroupLog;
import com.chlitina.store.modules.sms.entity.SmsStoreGroupLogDetail;
import com.chlitina.store.modules.sms.service.SmsStoreGroupLogDetailService;
import com.chlitina.store.modules.sms.service.SmsStoreGroupLogService;
import com.chlitina.store.modules.sys.entity.Store;

@Component
public class StoreGroupToCustomerQueueGenerate extends ManualSmsQueueGenrate {

	private String customerMobile = null;
	private Customer customer = null;
	private Store store = null;
	private Calendar cal = null;
	private String content = null;

	@Autowired
	private SmsStoreGroupLogService smsStoreGroupLogService;

	@Autowired
	private SmsStoreGroupLogDetailService smsStoreGroupLogDetailService;

	public StoreGroupToCustomerQueueGenerate() {
	}
	/**
	 * 加载log 和详细信息所需的字段
	 * @param store
	 * @param sendTime
	 * @param content
	 */
	public void load(Store store, String customerMobile, Customer customer, String content) {
		this.store = store;
		this.customerMobile = customerMobile;
		this.customer = customer;
		this.content = content;
	}
	/**
	 * 保存短信序列所需信息
	 */
	@Override
	public void saveQueue() {
		super.cal = cal;
		super.phone = customerMobile;
		super.content = content;
	}

	@Override
	public Long createLog() {
		SmsStoreGroupLog ssgl = new SmsStoreGroupLog();
		ssgl.setStore(store);
		ssgl.setPlanSendTime(cal.getTime());
		ssgl.setMsgContent(content);
		smsStoreGroupLogService.save(ssgl);
		return ssgl.getId();
	}

	@Override
	public void createDetail() {
		if (customer != null) {
			SmsStoreGroupLogDetail ssgld = new SmsStoreGroupLogDetail();
			SmsStoreGroupLog smsStoreGroupLog = smsStoreGroupLogService.get(createLog());
			ssgld.setSmsStoreGroupLog(smsStoreGroupLog);
			ssgld.setCustomer(customer);
			smsStoreGroupLogDetailService.save(ssgld);
		}
	}
	@Override
	public void setPlanSendTime(Calendar cal) {
		this.cal = cal;
	}
}
